import pyqtgraph as pg
import numpy as np
class ZMPWidget(pg.GraphicsLayoutWidget):
    def __init__(self,title='ZMP数据曲线',label="米/距中心",datSize=500,freq=50):
        super().__init__()
        self.xDat=[]
        self.yDat=[]
        self.dxDat=[]
        self.dyDat = []
        self.datSize=datSize
        self.freq=freq
        self.stamps=np.linspace(0,datSize/freq,datSize)
        self.title=title
        self.label=label
        self.InitDat()
    def InitDat(self):
        '''
        初始化
        :return:
        '''
        for i in range(self.datSize):
            self.xDat.append(0.0)
            self.yDat.append(0.0)
            self.dxDat.append(0.0)
            self.dyDat.append(0.0)
        self.canvas=self.addPlot(title=self.title)
        self.canvas.setLabel('left', self.label)
        self.canvas.addLegend()
        self.xCurve=self.canvas.plot(pen="r",name="X")
        self.xCurve.setData(self.stamps, self.xDat)
        self.dxCurve = self.canvas.plot(pen="b", name="dx")
        self.dxCurve.setData(self.stamps, self.dxDat)
        self.yCurve = self.canvas.plot(pen="y", name="Y")
        self.yCurve.setData(self.stamps, self.yDat)
        self.dyCurve = self.canvas.plot(pen="g", name="dy")
        self.dyCurve.setData(self.stamps, self.dyDat)



    def UpdateData(self,x,y,dx,dy):
        '''
        更新数据
        :param l:
        :param r:
        :param b:
        :return:
        '''
        self.xDat.append(x);
        self.xDat.pop(0)
        self.yDat.append(y);
        self.yDat.pop(0)
        self.dxDat.append(dx);
        self.dxDat.pop(0)
        self.dyDat.append(dy);
        self.dyDat.pop(0)
        self.xCurve.setData(self.stamps,self.xDat)
        self.yCurve.setData(self.stamps, self.yDat)
        self.dxCurve.setData(self.stamps, self.dxDat)
        self.dyCurve.setData(self.stamps, self.dyDat)




